//
//  ZLExamResultView.m
//  ZLExamination
//
//  Created by Zheng on 2017/8/10.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLExamResultView.h"
#import "ZLExamResultModel.h"

@interface ZLExamResultView ()

@property (nonatomic, strong) UIImageView *resultIconImgv; //结果表情图
@property (nonatomic, strong) UILabel *resultTitleLbl; //结果标题

@property (nonatomic, strong) UIImageView *bgImgv; //卡片背景
@property (nonatomic, strong) UILabel *studyLbl; //学时
@property (nonatomic, strong) UILabel *scoreLbl; //得分
@property (nonatomic, strong) UILabel *resultLbl; //考试结果
@property (nonatomic, strong) UILabel *trueLbl; //答对
@property (nonatomic, strong) UILabel *costTimeLbl; //耗时

@property (nonatomic, strong) UIButton *replyBtn; //重新考试

@end

@implementation ZLExamResultView

- (void)setModel:(ZLExamResultModel *)model {
    _model = model;
    
    _bgImgv.hidden = NO;
    
    _studyLbl.text = [NSString stringWithFormat:@"学时：%@",_model.credit_num];
    
    _scoreLbl.text = [NSString stringWithFormat:@"得分：%@",_model.result_num];
    
    _trueLbl.text = [NSString stringWithFormat:@"答对数/总数：%@/%@",_model.answer_num,_model.have_num];
    
    _costTimeLbl.text = [NSString stringWithFormat:@"用时：%@",[YCRequestDataService timeFormatHM:_model.the_time]];
    
    if (_model.is_pass.integerValue == 1) {
        //及格
        
        _replyBtn.hidden = YES;
        _resultTitleLbl.text = @"恭喜你，经本次测验合格";
        _resultIconImgv.image = kIMAGE(@"exam_r_t");
        _resultLbl.text = @"考试结果：合格";
        
        [_resultLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_bgImgv);
            make.centerY.equalTo(_bgImgv);
        }];

        
    } else {
        //不及格

        _replyBtn.hidden = NO;
        _resultTitleLbl.text = @"对不起，本次测验不合格";
        _resultIconImgv.image = kIMAGE(@"exam_r_f");
        _resultLbl.text = @"考试结果：不合格";
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"重新考试"];
        NSRange strRange = {0,[str length]};
        [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:strRange];
        [_replyBtn setAttributedTitle:str forState:UIControlStateNormal];
        
        [_resultLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_bgImgv);
            make.centerY.equalTo(_bgImgv).offset(-20);
        }];
        
        [_replyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_bgImgv);
            make.centerY.equalTo(_bgImgv).offset(20);
        }];

    }
    
}

#pragma mark - 点击事件
- (void)replyClickAction:(UIButton *)sender {
    if (_replyExamCallBack) {
        _replyExamCallBack();
    }
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.resultIconImgv];
        [self addSubview:self.resultTitleLbl];
        
        [self addSubview:self.bgImgv];
        [self addSubview:self.studyLbl];
        [self addSubview:self.scoreLbl];
        [self addSubview:self.resultLbl];
        [self addSubview:self.trueLbl];
        [self addSubview:self.costTimeLbl];
        
        [self addSubview:self.replyBtn];

        
        [_resultIconImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            make.width.height.mas_equalTo(75);
            make.centerX.equalTo(self);
        }];
        
        [_resultTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_resultIconImgv.mas_bottom).offset(10);
            make.centerX.equalTo(self);
        }];
        
        [_bgImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_resultTitleLbl.mas_bottom).offset(18);
            make.height.mas_equalTo(217);
            make.width.mas_equalTo(kSCREEN_WIDTH-20);
            make.centerX.equalTo(self);
        }];
        
        [_studyLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bgImgv).offset(14);
            make.top.equalTo(_bgImgv).offset(10);
        }];
        
        [_scoreLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_bgImgv).offset(-14);
            make.top.equalTo(_bgImgv).offset(10);
        }];
        
        [_trueLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bgImgv).offset(14);
            make.bottom.equalTo(_bgImgv).offset(-10);
        }];
        
        [_costTimeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_bgImgv).offset(-14);
            make.bottom.equalTo(_bgImgv).offset(-10);
        }];

    }
    return self;
}

- (UIButton *)replyBtn {
    if (!_replyBtn) {
        _replyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _replyBtn.titleLabel.font = kMyFont(33);
        NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"重新考试"];
        NSRange range = {0,[title length]};
        [title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range];
        [title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
        [_replyBtn setAttributedTitle:title  forState:UIControlStateNormal];
        [_replyBtn addTarget:self action:@selector(replyClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _replyBtn;
}

- (UIImageView *)resultIconImgv {
    if (!_resultIconImgv) {
        _resultIconImgv = [[UIImageView alloc] init];
    }
    return _resultIconImgv;
}

- (UILabel *)resultTitleLbl {
    if (!_resultTitleLbl) {
        _resultTitleLbl = [[UILabel alloc] init];
        _resultTitleLbl.font = kCX_FONT(18);
    }
    return _resultTitleLbl;
}

- (UILabel *)trueLbl {
    if (!_trueLbl) {
        _trueLbl = [[UILabel alloc] init];
        _trueLbl.font = kCX_FONT(16);
        _trueLbl.textColor = [UIColor whiteColor];
    }
    return _trueLbl;
}

- (UILabel *)costTimeLbl {
    if (!_costTimeLbl) {
        _costTimeLbl = [[UILabel alloc] init];
        _costTimeLbl.font = kCX_FONT(16);
        _costTimeLbl.textColor = [UIColor whiteColor];
    }
    return _costTimeLbl;
}

- (UILabel *)resultLbl {
    if (!_resultLbl) {
        _resultLbl = [[UILabel alloc] init];
        _resultLbl.font = kMyFont(33);
        _resultLbl.textColor = [UIColor whiteColor];
    }
    return _resultLbl;
}

- (UILabel *)studyLbl {
    if (!_studyLbl) {
        _studyLbl = [[UILabel alloc] init];
        _studyLbl.font = kMyFont(18);
        _studyLbl.textColor = [UIColor whiteColor];
    }
    return _studyLbl;
}

- (UILabel *)scoreLbl {
    if (!_scoreLbl) {
        _scoreLbl = [[UILabel alloc] init];
        _scoreLbl.font = kMyFont(18);
        _scoreLbl.textColor = [UIColor whiteColor];
    }
    return _scoreLbl;
}

- (UIImageView *)bgImgv {
    if (!_bgImgv) {
        _bgImgv = [[UIImageView alloc] init];
        _bgImgv.image = kIMAGE(@"result_bg");
        _bgImgv.hidden = YES;
    }
    return _bgImgv;
}






@end
